package com.stylefeng.guns.modular.system.task;

import java.net.URL;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.model.Brank;
import com.stylefeng.guns.modular.system.model.BusinessRankingData;
import com.stylefeng.guns.modular.system.service.IBrankService;

@Service
public class BrankImportTask {

	private Logger log = LoggerFactory.getLogger(BrankImportTask.class);
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	@Autowired
	private IBrankService brankService;

	public void execute(boolean isHoliday) {
		if(isHoliday){
			String day = DateUtils.formatDate(new Date(), "yyyy-MM-dd");
			String month3Day = getBeforeDate(-6);
			String url = ImportContants.getFormatBrankPathStr(1,month3Day, day);
			HttpGet httpGet = HttpClientUtil.getGetRequest();
			try {
				httpGet.setURI(new URL(url).toURI());
				String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
				JSONObject jsonObj = JSON.parseObject(StringUtils.replace(StringUtils.substringAfter(json, "="), "\\", ""));
				if (jsonObj.getBooleanValue("success")) {
					parseRankData(jsonObj.getString("data"));
					int pages = jsonObj.getIntValue("pages");
					for(int i= 2; i <= pages ; i++){
						url = ImportContants.getFormatBrankPathStr(i ,month3Day, day);
						httpGet.setURI(new URL(url).toURI());
						json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
						jsonObj = JSON.parseObject(StringUtils.replace(StringUtils.substringAfter(json, "="), "\\", ""));
						if (jsonObj.getBooleanValue("success")) {
							parseRankData(jsonObj.getString("data"));
						}
					}
				}
			} catch (Exception e) {
				log.error("get brank data is error!" + e);
			}finally{
				httpGet.abort();
			}
		}
	}
	
	private void parseRankData(String json){
		if(StringUtils.isNotEmpty(json)){
			List<BusinessRankingData> channelItemList = JSON.parseArray(json,BusinessRankingData.class);
			for(BusinessRankingData businessRankingData : channelItemList){
				Wrapper<Brank> wrapper = new EntityWrapper<Brank>();
				wrapper.eq("g_sales_code", businessRankingData.getSalesCode());
				Brank brank = brankService.selectOne(wrapper);
				if(brank == null) brank = new Brank();
				brank.setgSalesName(businessRankingData.getSalesName());
				brank.setgSalesCode(businessRankingData.getSalesCode());
				brank.setgAvgRate15dc(businessRankingData.getAvgRate15DC());
				brank.setgAvgRate1dc(businessRankingData.getAvgRate1DC());
				brank.setgAvgRate2dc(businessRankingData.getAvgRate2DC());
				brank.setgAvgRate3dc(businessRankingData.getAvgRate3DC());
				brank.setgAvgRate5dc(businessRankingData.getAvgRate5DC());
				brank.setgAvgRate30dc(businessRankingData.getAvgRate30DC());
				brank.setgBcount15dc(businessRankingData.getBCount15DC());
				brank.setgBcount1dc(businessRankingData.getBCount1DC());
				brank.setgBcount2dc(businessRankingData.getBCount2DC());
				brank.setgBcount3dc(businessRankingData.getBCount3DC());
				brank.setgBcount5dc(businessRankingData.getBCount5DC());
				brank.setgBcount30dc(businessRankingData.getBCount30DC());
				brank.setgUpRate15dc(businessRankingData.getUpRate15DC());
				brank.setgUpRate1dc(businessRankingData.getUpRate1DC());
				brank.setgUpRate2dc(businessRankingData.getUpRate2DC());
				brank.setgUpRate3dc(businessRankingData.getUpRate3DC());
				brank.setgUpRate5dc(businessRankingData.getUpRate5DC());
				brank.setgUpRate30dc(businessRankingData.getUpRate30DC());
				brankService.insertOrUpdate(brank);
			}
		}
	}
	
	private String getBeforeDate(int month){
		return DateUtils.formatDate(org.apache.commons.lang3.time.DateUtils.addMonths(new Date(), month), "yyyy-MM-dd");
	}
}
